package com.mobile.challenge.ui.report.unit.admin.weekmonth

import android.os.Bundle
import androidx.lifecycle.Observer
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.components.XAxis
import com.github.mikephil.charting.formatter.ValueFormatter
import com.mobile.challenge.R
import com.mobile.challenge.databinding.FragmentHistogramBinding
import com.mobile.challenge.ui.report.adapter.IRefresh
import com.mobile.challenge.ui.report.unit.admin.AdminUtils
import com.mobile.frame.mvvmframe.base.BaseFragment
import java.util.*

/**
 * @Author Touge
 * @Date 2020/9/16 21:51
 * @Description
 */
class AdminHistogramFragment : BaseFragment<AdminHistogramViewModel, FragmentHistogramBinding>(),
        IRefresh {

    private lateinit var unitName: String

    companion object {
        const val UNIT_Name = "unit_name"
        fun newInstance(unitName: String): AdminHistogramFragment {
            val args = Bundle()
            args.putString(UNIT_Name, unitName)
            val fragment = AdminHistogramFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            unitName = it.getString(UNIT_Name, "")
        }
    }


    override fun getLayoutId(): Int {
        return R.layout.fragment_histogram
    }

    override fun initData(savedInstanceState: Bundle?) {
        initBarChart()
        mViewModel.barData.observe(this, Observer { data ->
            mBinding.barChart.data = data
            mBinding.barChart.animateXY(1000, 1000)
        })
        mViewModel.generateData(unitName)
    }

    private fun initBarChart() {
        with(mBinding.barChart) {
            description.setPosition(resources.displayMetrics.widthPixels / 2f, 48f)
            description.textSize = 18F
            description.text = unitName
            setExtraOffsets(5f, 15f, 5f, 5f)
            // X 轴 显示位置
            xAxis.position = XAxis.XAxisPosition.BOTTOM
            // 取消网格线
            xAxis.setDrawGridLines(false)
            // X 坐标居中
            xAxis.setCenterAxisLabels(true)
            // 设置 x 坐标轴 宽度
            xAxis.axisLineWidth = 2f
            // 设置 x轴 的刻度数量
//            xAxis.labelCount = 5
            xAxis.labelRotationAngle = 90F

            xAxis.valueFormatter = object : ValueFormatter() {

                override fun getFormattedValue(value: Float): String {
                    return AdminUtils.getInstance().getDateStr(value.toInt())
                }
            }

            // 隐藏右边 Y 轴
            axisRight.isEnabled = false
            axisLeft.axisMinimum = 0f

            // 图例
            legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
            legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
            legend.orientation = Legend.LegendOrientation.VERTICAL
            legend.setDrawInside(true)
            legend.yOffset = 0f
            legend.xOffset = 10f
            legend.yEntrySpace = 0f
            legend.textSize = 8f
        }
    }

    override fun refresh(calendar: Calendar, period: Int) {
        if (mViewModel != null) {
            mViewModel.generateData(unitName)
        }
    }
}